/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class UDPSocketCommunicator.
//
// UDPPacketSocketCommunicator uses to send/receive UDP packet.
// This UDP uses packet pattern, each send will packet a UDP packet.
//

#include "udp_packet_socket_communicator.h"
#include "packet_define.h"

namespace amz {

UDPPacketSocketCommunicator::UDPPacketSocketCommunicator(
       const sockaddr_in &address, int len, UDPSocket *udp_socket,
       CompressType compress, EncryptType encrypt, int flag)
       : UDPSocketCommunicator(compress, encrypt, address, len, udp_socket, flag),
         max_message_size_(packet_.CalculateUnpacketSize(packet::kMTU_UDP)),
         send_buffer_(max_message_size_*packet::kDefaultPacketNumber, max_message_size_),
         receive_buffer_(max_message_size_*packet::kDefaultPacketNumber, max_message_size_) {
  if(this->SetDestinationAddress(&address, len) != 0) 
    LOG(ERROR) << "Failed to SetDestinationAddress.";
}

UDPPacketSocketCommunicator::~UDPPacketSocketCommunicator() {}

int UDPPacketSocketCommunicator::Send(const char *message, int size) {
  if((uint32)size > this->max_message_size_) {
    LOG(ERROR) << "Failed to Send, user message is too long!";
    return -1;
  }
  // Calculate packet size, allocate, packet.
  int need_size = this->packet_.CalculatePacketSize(size);
  if(this->buffer_.PreWrite(need_size) == false) {
    LOG(ERROR) << "Failed to Send, buffer_ size is not enough!";
    return -1;
  }
  int packet_size = this->packet_.Packet(message, size,
         this->buffer_.HeadPointer(), need_size);
  if(packet_size <= 0) {
    return packet_size;
  }
  // Add to send_buffer_
  if(this->send_buffer_.Add(this->buffer_.HeadPointer(), packet_size) != packet_size) {
    LOG(ERROR) << "Failed to Send, error in add to send_buffer_!";
    return -1;
  }

  return size;
}

int UDPPacketSocketCommunicator::Receive(char *message, int max_size) {
  return this->receive_buffer_.Remove(message, max_size);
}

int UDPPacketSocketCommunicator::Send() {
  if(this->buffer_.PreWrite(packet::kMTU_UDP) == false) {
    LOG(ERROR) << "Failed to Receive, buffer_ size is not enough!";
    return -1;
  }
  int total_send_size = 0, send_size = 0;
  while(true) {
    send_size = this->send_buffer_.Remove(this->buffer_.HeadPointer(), packet::kMTU_UDP);
    if(send_size <= 0) {
      break;
    }
    send_size = this->udp_socket_->Send(this->buffer_.HeadPointer(), send_size,
           (const struct sockaddr *)&this->address_in4_, sizeof(struct sockaddr), this->flag_);
    if(send_size <= 0) {
      break;
    }
    total_send_size += send_size;
  }
  return total_send_size;
}

int UDPPacketSocketCommunicator::PushReceive(char *message, int size) {
  // Calculate unpacket size.
  int need_size = this->packet_.CalculateUnpacketSize(size);
  if(this->buffer_.PreWrite(need_size) == false) {
    LOG(ERROR) << "Failed to PushReceive, buffer_ size is not enough!";
    return -1;
  }
  // Unpacket.
  int packet_size = this->packet_.Unpacket(message, size,
         this->buffer_.HeadPointer(), need_size);
  if(packet_size <= 0) {
    return packet_size;
  }
  // Add to buffer.
  if(this->receive_buffer_.Add(this->buffer_.HeadPointer(), packet_size) != packet_size) {
    LOG(ERROR) << "Failed to PushReceive, error in add to receive_buffer_!";
    return -1;
  }
  return size;
}

}  // namespace amz

